using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace GameCore.UI
{
    public class SelectableIcon : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler
    {
        public Action OnSelected;
        public Action OnDeselected;
        
        public bool isSelected = false;
        
        public Transform selectedIndicator;
        public Image iconImage;
        
        public Sprite normalSprite;
        public Sprite hoverSprite;
        
        public void Select()
        {
            isSelected = true;
            iconImage.sprite = hoverSprite;
            selectedIndicator.localScale = Vector3.one;
            OnSelected?.Invoke();
        }
        
        public void Deselect()
        {
            isSelected = false;
            iconImage.sprite = normalSprite;
            selectedIndicator.localScale = Vector3.zero;
            OnDeselected?.Invoke();
        }
        
        public void OnPointerEnter(PointerEventData eventData)
        {
            if (isSelected) return;
            iconImage.sprite = hoverSprite;
        }

        public void OnPointerExit(PointerEventData eventData)
        {
            if (isSelected) return;
            iconImage.sprite = normalSprite;
        }

        public void OnPointerDown(PointerEventData eventData)
        {
            Select();
        }
    }
}